package com.isoft.game.sys.role.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.isoft.game.common.constant.MessageConstant;
import com.isoft.game.common.controller.PaginationController;
import com.isoft.game.common.message.AjaxResponse;
import com.isoft.game.common.model.GridModel;
import com.isoft.game.common.model.PersistModel;
import com.isoft.game.sys.role.entity.Role;
import com.isoft.game.sys.role.service.RoleService;

@Controller
@RequestMapping("/admin/role")
public class RoleController extends PaginationController<Role> {

    @Autowired
    private RoleService roleServiceImpl;


    /**
     * 分页查询角色
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("queryRoles")
    public GridModel queryRoles(Role role){
        roleServiceImpl.queryRolesByPagination(getPaginationUtility(),role);
        GridModel gridModel = getGridModelResponse();
        return  gridModel;
    }

    
    /**
     * 增加
     * @param
     * @return
     */
    @RequestMapping("roleAdd")
    @ResponseBody
    public AjaxResponse roleAdd( Role role, BindingResult br, AjaxResponse ajaxResponse){
        PersistModel persistModel = roleServiceImpl.persistRole(role, super.getUserInfo());
        if(persistModel.isSuccessBySinglePersist())
            ajaxResponse.setSuccessMessage(MessageConstant.MESSAGE_ALERT_SUCCESS,role.getBusinessId());
        else
            ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,MessageConstant.MESSAGE_ALERT_ERROR);
        return ajaxResponse;
    }

    /**
     * to 修改用户页面
     * @param id
     * @param modelAndView
     * @return
     */
    @RequestMapping("toBg/toRoleUpdatePage/{id}")
    public ModelAndView toUserUpdatePage(@PathVariable("id") String id, ModelAndView modelAndView){
        modelAndView.getModelMap().addAttribute("roleUpdate",roleServiceImpl.queryRoleById(id));
        modelAndView.setViewName("background/roleAdd");
        return modelAndView;
    }
    
    /**
     * 修改
     * @param 
     * @return
     */
    @RequestMapping("roleUpdate")
    @ResponseBody
    public AjaxResponse roleUpdate( Role role, BindingResult br, AjaxResponse ajaxResponse){
        /*if(super.hasErrors(br)){
            ValidatedResult vr = new ValidatedResult();
            vr.dealBindingResult(br);
            ajaxResponse.setSuccessMessage(MessageConstant.MESSAGE_ALERT_INFO_INVALID,vr);
            return ajaxResponse;
        }*/
        PersistModel persistModel = roleServiceImpl.mergeRole(role, super.getUserInfo());
        if(persistModel.isSuccessBySinglePersist())
            ajaxResponse.setSuccessMessage(MessageConstant.MESSAGE_ALERT_SUCCESS,role.getBusinessId());
        else
            ajaxResponse.setErrorMessage(MessageConstant.MESSAGE_ALERT_ERROR,MessageConstant.MESSAGE_ALERT_ERROR);
        return ajaxResponse;
    }
    
}
